package com.lc202408;

public class Solution463 {
    public int islandPerimeter(int[][] grid) {
        int y = grid.length;
        int x = grid[0].length;
        int res = 0;
        for (int i = 0; i < y; i++) {
            for (int j = 0; j < x; j++) {
                        if (grid[i][j] == 1) {
                            if (i < 1 || grid[i - 1][j] == 0) {
                                res++;
                            }
                            if (i + 1 == y || grid[i + 1][j] == 0) {
                                res++;
                    }
                    if (j < 1 || grid[i][j - 1] == 0) {
                        res++;
                    }
                    if (j + 1 == x || grid[i][j + 1] == 0) {
                        res++;
                    }
                }
            }
        }
        return res;
    }
}
